package net.zabuchy.meeting.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.zabuchy.address.entity.Address;
import net.zabuchy.helper.AddressHelper;
import net.zabuchy.helper.ParamNames;
import net.zabuchy.helper.SessionHelper;
import net.zabuchy.meeting.bo.MeetingBo;
import net.zabuchy.meeting.entity.Meeting;
import net.zabuchy.user.bo.UserBo;
import net.zabuchy.user.entity.User;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class EditMeetingAction extends ActionSupport
		implements ServletRequestAware,ServletResponseAware{
	
private static final Logger LOGGER = Logger.getLogger(CreateMeetingAction.class.getName());
	
	@Autowired
	MeetingBo meetingBo;
	@Autowired
	UserBo userBo;
	@Autowired
    private SessionHelper sessionBean;
	
	private HttpServletRequest request;
    private HttpServletResponse response;
    
    Meeting meeting;
	
	private Integer meetingId;
	private Integer customerId;
	private String author;
	private String title;
	private String description;
	private String leading;
	private String leadingChoose;
	private Date creationDate;
	private Date meetingDate;
	private String meetingTime;
	private String leadingNickname;
	private String place;
	
	private Address address;
	
	private String tmpParticipants;
	
	private Set<User> participants = new HashSet<User>(0);
	private String participant;
	
	/**********************************************/
	private String city;
	private String country;
	private String address1;
	private String state;
	private String zipcode;
	private String[] countries = AddressHelper.getCountries();
	/**********************************************/
	
	private List<String> users = new ArrayList<String>(0);
	private Integer userId;
	
	
	public String execute() {
		if (meetingBo == null || meetingId == 0) {
			return Action.ERROR;
		}
		LOGGER.debug("editForm");
		
		String returnInputMeetingForm = request.getParameter(ParamNames.EDIT_MEETING_FORM);
		
		boolean returnForm = (returnInputMeetingForm != null ? 
				Boolean.parseBoolean(returnInputMeetingForm) : false);
		
		List<User> tmp = userBo.findByCustomerId(sessionBean.getCustomerId());
		Iterator it = tmp.iterator();
		while(it.hasNext()) {
			User user = (User) it.next();
			users.add(user.getUsername());
		}
		
		if(returnForm) {
			LOGGER.debug("clear all fields and return form");
			clearFields();
			getMeeting(meetingId);
			return Action.INPUT;
		}
		
		if(leadingChoose.equals("1")) {
			leading =  sessionBean.getExecutor().getUsername();
		} else {
			leading = leadingNickname;
		}
		LOGGER.debug("meetingdate: " + meetingDate);
		meeting = meetingBo.findById(meetingId);
		meeting.setTitle(title);
		meeting.setDescription(description);
		meeting.setMeetingDate(meetingDate);
		meeting.setMeetingTime(meetingTime);
		meeting.setPlace(place);
		meeting.setLeading(leading);
		
		meetingBo.update(meeting);
		
		return Action.SUCCESS;
	}
	
	public String editMeetingAddress() {
		LOGGER.debug("editMeetinAddress start");
		if(meeting.getAddress() != null) {
			address = meeting.getAddress();
		} else {
			address = new Address();
		}
		
		address.setAddress1(address1);
		address.setCity(city);
		address.setCountry(country);
		address.setState(state);
		address.setZipcode(zipcode);
		
		meetingBo.update(meeting);
		return Action.SUCCESS;
	}
	
	public String editMeetingParticipants() {
		
		LOGGER.debug("editMeetinAddress start");
		User user = userBo.findByUsername(participant);
		participants.add(user);
		meeting.setParticipants(participants);
		meetingBo.update(meeting);
		
		return Action.SUCCESS;
	}
	
	public String removeParticipants() {
		
		if (userBo == null) {
            return Action.ERROR;
        }
        User user = userBo.findById(userId);
        if (user == null) {
            return Action.ERROR;
        }
        
        LOGGER.debug("Remove user " + user.getUsername() + " with id " + user.getUserId() + " from meeting: " + meetingId);
        LOGGER.debug("participants.size: " + participants.size());
        Iterator it = participants.iterator();
        Set<User> temporary = new HashSet<User>(0);
        while(it.hasNext()) {
        	User tmp = (User) it.next();
        	if(user.getUserId() != tmp.getUserId()) {
        		temporary.add(tmp);
        	}
        }
        participants.clear();
        participants.addAll(temporary);
        LOGGER.debug("participants.size: " + participants.size());
        meeting.setParticipants(participants);
        meetingBo.update(meeting);
        
		return Action.SUCCESS;
	}
	
	public void clearFields() {
		this.title = null;
		this.description = null;
		this.leadingChoose = null;
		this.meetingDate = null;
		this.meetingTime = null;
		this.leadingNickname = null;
		this.place = null;
		
		this.participants.clear();
		
		/**********************************************/
		Address address = null;
		this.city = null;
		this.country = null;
		this.address1 = null;
		this.state = null;
		this.zipcode = null;
	}
	
	public void getMeeting(int meetingId) {
		LOGGER.debug("getting meeting with id: " + meetingId);
		meeting = meetingBo.findById(meetingId);
		this.title = meeting.getTitle();
		this.description = meeting.getDescription();
		this.leadingChoose = "2";
		this.meetingDate = meeting.getMeetingDate();
		this.meetingTime = meeting.getMeetingTime();
		this.leadingNickname = meeting.getLeading();
		this.place = meeting.getPlace();
		
		
		this.participants = meeting.getParticipants();///////////////////////
		
		/**********************************************/
		Address address = meeting.getAddress();
		if(address != null) {
			this.city = address.getCity();
			this.country = address.getCountry();
			this.address1 = address.getAddress1();
			this.state = address.getState();
			this.zipcode = address.getZipcode();
		}
	}
	
	public void setParticipants(Set<User> participants) {
		this.participants = participants;
	}

	public MeetingBo getMeetingBo() {
		return meetingBo;
	}

	public void setMeetingBo(MeetingBo meetingBo) {
		this.meetingBo = meetingBo;
	}

	public UserBo getUserBo() {
		return userBo;
	}

	public void setUserBo(UserBo userBo) {
		this.userBo = userBo;
	}

	public SessionHelper getSessionBean() {
		return sessionBean;
	}

	public void setSessionBean(SessionHelper sessionBean) {
		this.sessionBean = sessionBean;
	}

	public Integer getMeetingId() {
		return meetingId;
	}

	public void setMeetingId(Integer meetingId) {
		this.meetingId = meetingId;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getLeadingChoose() {
		return leadingChoose;
	}

	public void setLeadingChoose(String leadingChoose) {
		this.leadingChoose = leadingChoose;
	}

	public Date getMeetingDate() {
		return meetingDate;
	}

	public void setMeetingDate(Date meetingDate) {
		this.meetingDate = meetingDate;
	}

	public String getMeetingTime() {
		return meetingTime;
	}

	public void setMeetingTime(String meetingTime) {
		this.meetingTime = meetingTime;
	}

	public String getLeadingNickname() {
		return leadingNickname;
	}

	public void setLeadingNickname(String leadingNickname) {
		this.leadingNickname = leadingNickname;
	}

	public String getPlace() {
		return place;
	}

	public void setPlace(String place) {
		this.place = place;
	}

	public Address getAddress() {
		return address;
	}

	public void setAddress(Address address) {
		this.address = address;
	}

	public String getParticipant() {
		return participant;
	}

	public void setParticipant(String participant) {
		this.participant = participant;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getAddress1() {
		return address1;
	}

	public void setAddress1(String address1) {
		this.address1 = address1;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getZipcode() {
		return zipcode;
	}

	public void setZipcode(String zipcode) {
		this.zipcode = zipcode;
	}

	public String[] getCountries() {
		return countries;
	}

	public void setCountries(String[] countries) {
		this.countries = countries;
	}

	public List<String> getUsers() {
		return users;
	}

	public void setUsers(List<String> users) {
		this.users = users;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public Set<User> getParticipants() {
		return participants;
	}

	public void setServletRequest(HttpServletRequest request){
		  this.request = request;
		  }

		  public HttpServletRequest getServletRequest(){
		  return request;
		  }

		  public void setServletResponse(HttpServletResponse response){
		  this.response = response;
		  }

		  public HttpServletResponse getServletResponse(){
		  return response;
		  }


	
}